package com.bulletelc.apollopre.module.user;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.adapter.ViewPageAdapter;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.eventbus.LoginEvent;
import com.bulletelc.apollopre.framework.BaseActivity;
import com.bulletelc.apollopre.interfaces.listener.PermissionListener;
import com.bulletelc.apollopre.module.main.MainActivity;
import com.bulletelc.apollopre.module.user.FastRegister.FastRegisterFragment;
import com.bulletelc.apollopre.module.user.login.LoginFragment;
import com.bulletelc.apollopre.utils.MyUtil;
import com.bulletelc.apollopre.utils.PhoneUtil;
import com.bulletelc.apollopre.widget.FoolBar;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CX on 2018/3/29.
 */

public class LoginMainActivity extends BaseActivity implements LoginFragment.checkPermission
        , LoginFragment.iFinish, PermissionListener, FoolBar.ClickCallback {

    // views
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FoolBar foolBar;
    private ViewPageAdapter pageAdapter;
    
    // data
    private List<Fragment> fragments;
    private List<String> title;
    private String unionid = null;
    private String openType; //  标识哪个activity打开此界面
    private boolean isGoToHome = false; // 当退出登录页,是否返回首页

    @Override
    public int getLayoutRes() {
        return R.layout.login_activity;
    }

    @Override
    public void assignViews() {
        tabLayout = findView(R.id.tlMain);
        viewPager = findView(R.id.vpMain);
        foolBar = findView(R.id.flBar);
        foolBar.setTitle(this.getResources().getString(R.string.login_main_title));
        foolBar.setDrawableLeft(this.getResources().getDrawable(R.drawable.sel_close));
        foolBar.setBorderLine(false);
    }

    @Override
    public void setListener() {
        foolBar.setClickCallback(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (null != intent) {
            openType = intent.getStringExtra(IntentCode.EXTRA_KEY_OPEN_LOGIN_TYPE);
            isGoToHome = intent.getBooleanExtra(IntentCode.EXTRA_KEY_IS_GO_TO_HOME_FRAGMENT, false);
        }

        fragments = new ArrayList<>();
        title = new ArrayList<>();
        fragments.add(LoginFragment.getInstance());
        fragments.add(FastRegisterFragment.getInstance());
        title.add("账号登陆");
        title.add("立即注册");
        pageAdapter = new ViewPageAdapter(getSupportFragmentManager(), fragments, title);
        viewPager.setAdapter(pageAdapter);
        tabLayout.setupWithViewPager(viewPager);
        MyUtil.reflex(tabLayout);
    }

    @Override
    public void checkPermission() {
        requestRuntimePermission(new String[]{Manifest.permission.READ_PHONE_STATE}, this);
    }

    @Override
    public void onGranted() {
        String equipment_id = PhoneUtil.getIMEI(this);
        EventBus.getDefault().post(new LoginEvent(equipment_id));
    }

    @Override
    public void onDenied(List<String> deniedPermissions) {
        EventBus.getDefault().post(null);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        onBack();
    }

    @Override
    public void ifinish() {
        finish();
    }

    @Override
    public void onLeftClick() {
        onBack();
    }

    @Override
    public void onRightClick() {

    }

    /**
     * 退出该界面时执行的代码逻辑
     */
    private void onBack() {
        if (isGoToHome) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra(IntentCode.EXTRA_KEY_FRAGMENT_POSITION, 0);
            startActivity(intent);
        }
        this.finish();
    }

    public String getUnionid() {
        return unionid;
    }

    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }
}
